package com.zcm.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcm.service.CommodityService;
import com.zcm.service.UserService;

/**
 * Servlet implementation class DoAddFavorite
 */
@WebServlet("/DoAddFavorite")
public class DoAddFavorite extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddFavorite() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userid = Integer.parseInt(request.getParameter("userid"));
		int comid = Integer.parseInt(request.getParameter("comid"));
		UserService us = new UserService();
		CommodityService cs = new CommodityService();
		if(us.FavoriteIsExist(comid, userid)>0) {
			request.setAttribute("msg", "已收藏有该商品！");
			List listforcommodity= new ArrayList<>();
			listforcommodity = cs.SelectAllcommodity();
			request.setAttribute("lc",listforcommodity);
			request.setAttribute("userid", userid);
			request.getRequestDispatcher("/indexforafterlogin.jsp").forward(request, response);
		}else {
			request.setAttribute("msg","商品添加成功");
			us.AddFavorite(userid, comid);
			List listforcommodity= new ArrayList<>();
			listforcommodity = cs.SelectAllcommodity();
			request.setAttribute("lc",listforcommodity);
			request.setAttribute("userid", userid);
			request.getRequestDispatcher("/indexforafterlogin.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
